<%@page import="java.lang.reflect.Array"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="cn.com.xf.utils.DateUtils"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.apache.commons.lang.time.DateFormatUtils"%>
<%@page import="org.springframework.util.CollectionUtils"%>
<%@page import="java.util.Collection"%>
<%@ include file="../common/common_include.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%!public int getMax(List<ArrayList<TblOaDuty>> list) {
	int len = list.get(0).size();
	for (int i = 1; i < list.size(); i++) {
		if (list.get(i).size() > len) {
			len = list.get(i).size();
		}
	}
	return len;
	}
%>
<%
	String isAll = (String) request.getAttribute("isAll");
	Map<String,List<ArrayList<TblOaDuty>>> scheduleMap = null;
	Map<String, Map<String,List<ArrayList<TblOaDuty>>>> scheduleMap2 = null;
	if(isAll.equals("no")){
		scheduleMap = (Map<String,List<ArrayList<TblOaDuty>>>) request.getAttribute("dutyMap");
	}else if(isAll.equals("yes")){
		scheduleMap2 = (Map<String, Map<String,List<ArrayList<TblOaDuty>>>>) request.getAttribute("dutyMap");
	}
	DutyForm form = (DutyForm) request.getAttribute("command");
	UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	
	SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat formater2 = new SimpleDateFormat("MM/dd");
	/* 获取当前页的查询参数 */
	String dutyTime = form.getDutyDate();
	String siteId = form.getSiteId();
	String deptId = form.getDeptId();
	String siteName = userInfo.getSiteName();
	//时间处理
	Date date = formater.parse(dutyTime);
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	//上一周
	cal.add(Calendar.DATE, -7);
	String preWeek = formater.format(cal.getTime());
	//下一周
	cal.add(Calendar.DATE, 14);
	String nextWeek = formater.format(cal.getTime());
	cal.setTime(date);
	//封装表头列信息
	String[] strs = new String[6];
	for (int i = 0; i < 6; i++) {
		cal.add(Calendar.DATE, 1);
		strs[i] = formater2.format(cal.getTime());
	}
%>
		<table class="tableList" style="width:100%">
			<thead>
				<tr>
					<td colspan="11" style="width: 100%;">
						<div class="box FormTools">
							<div>
								时间： <input id="dutyTime" name="dutyDate" type="text" class="easyui-datebox" />
								 <input type="button" class="searchSubmit"
									onclick="javascript:XF.Duty.dutyListSearch()" value="搜索" />
							</div>
							<div>
								部门： <select id="deptList" class="easyui-combobox" name="deptId"
									style="width: 100px;"></select>
							</div>
							<div>
								所属公司： <select id="siteList" class="easyui-combobox"
									name="siteId" style="width: 100px;"></select>
							</div>
						</div>
					</td>
				</tr>
			</thead>
			<tbody>
				<tr id="tableHeader">
					<th style="width: 2%">编号</th>
					<th style="width: 8%">公司</th>
					<th style="width: 5%">部门</th>
					<th style="width: 5%">班次</th>
					<th style="width: 5%">星期一<br /><%=formater2.format(date)%></th>
					<th style="width: 5%">星期二<br /><%=strs[0]%></th>
					<th style="width: 5%">星期三<br /><%=strs[1]%></th>
					<th style="width: 5%">星期四<br /><%=strs[2]%></th>
					<th style="width: 5%">星期五<br /><%=strs[3]%></th>
					<th style="width: 5%">星期六<br /><%=strs[4]%></th>
					<th style="width: 5%">星期日<br /><%=strs[5]%></th>
				</tr>
				<%
					int i = 1;
					int rows = 0;
					//输出所有部门
					if(isAll.equals("yes")){
						Set<String> deptSet = scheduleMap2.keySet();
						for(String deptName : deptSet){
							rows = 0;
							Map<String,List<ArrayList<TblOaDuty>>> deptMap = scheduleMap2.get(deptName);
							boolean siteInfo = false;
							
							//注意---------------------------修改部分
							Map<String, Integer> lenMap = new HashMap<String, Integer>();
							Set<String> keySet = deptMap.keySet();
							//部门的班次
							for(String scheduleId : keySet){
								lenMap.put(scheduleId, getMax(deptMap.get(scheduleId)));
							}
							
							Set<String> keySet2 = lenMap.keySet();
							for(String id : keySet2){
								rows += lenMap.get(id);
							}
							
							for(String id:keySet){
								int thislen =lenMap.get(id);
								String scheduleName = "";
								String deptTelNo = "";
								String startTime = "";
								String endTime = "";
								List<ArrayList<TblOaDuty>> list = deptMap.get(id);
								for(int k=0;k<list.size();k++){
									if(list.get(k).size()>0){
									scheduleName = list.get(k).get(0).getScheduleName();
									deptTelNo = list.get(k).get(0).getDeptTelNo();
									startTime =list.get(k).get(0).getStartTime();
									endTime = list.get(k).get(0).getEndTime();
									break;
									}else {
										continue;
									}
								}
								for (int j = 0; j < thislen; j++) {
							%>
							<tr>
								<td><%=i%></td>
								<%if(!siteInfo&&j==0){%>
								<td rowspan='<%=rows%>'><%=siteName%></td>
								<td rowspan='<%=rows%>'><%=deptName%><br/><%=deptTelNo%></td>
								<% 
									siteInfo=true;
								} %>
								
								<%if(j==0){%>
								<td rowspan='<%=thislen%>'><%=scheduleName%><br/>(<%=startTime%>-<%=endTime%>)</td>
								<% } %>
								<td><%=(list.get(0).size() != 0) && (list.get(0).size() > j)
										? list.get(0).get(j).getUserName()+"<br/>"+list.get(0).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(1).size() != 0) && (list.get(1).size() > j)
										? list.get(1).get(j).getUserName()+"<br/>"+list.get(1).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(2).size() != 0) && (list.get(2).size() > j)
										? list.get(2).get(j).getUserName()+"<br/>"+list.get(2).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(3).size() != 0) && (list.get(3).size() > j)
										? list.get(3).get(j).getUserName()+"<br/>"+list.get(3).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(4).size() != 0) && (list.get(4).size() > j)
										? list.get(4).get(j).getUserName()+"<br/>"+list.get(4).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(5).size() != 0) && (list.get(5).size() > j)
										? list.get(5).get(j).getUserName()+"<br/>"+list.get(5).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(6).size() != 0) && (list.get(6).size() > j)
										? list.get(6).get(j).getUserName()+"<br/>"+list.get(6).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
							</tr>
					<%
							i++;
								}
							}
						}
					}else {
						rows = 0;
						boolean siteInfo = false;
						Map<String, Integer> lenMap = new HashMap<String, Integer>();
						Set<String> keySet = scheduleMap.keySet();
						for(String scheduleId : keySet){
							lenMap.put(scheduleId, getMax(scheduleMap.get(scheduleId)));
						}
						
						Set<String> keySet2 = lenMap.keySet();
						for(String id : keySet2){
							rows += lenMap.get(id);
						}
						
						for(String id:keySet){
							int thislen =lenMap.get(id);
							String scheduleName = "";
							String deptTelNo = "";
							String startTime = "";
							String endTime = "";
							List<ArrayList<TblOaDuty>> list = scheduleMap.get(id);
							for(int k=0;k<list.size();k++){
								if(list.get(k).size()>0){
								scheduleName = list.get(k).get(0).getScheduleName();
								deptTelNo = list.get(k).get(0).getDeptTelNo();
								startTime =list.get(k).get(0).getStartTime();
								endTime = list.get(k).get(0).getEndTime();
								break;
								}else {
									continue;
								}
							}
							for (int j = 0; j < thislen; j++) {
						%>
							<tr>
								<td><%=i%></td>
								<%if(!siteInfo&&j==0){%>
								<td rowspan='<%=rows%>'><%=siteName%></td>
								<td rowspan='<%=rows%>'><%=form.getDeptName()%><br/><%=deptTelNo%></td>
								<% 
									siteInfo=true;
								} %>
								
								<%if(j==0){%>
								<td rowspan='<%=thislen%>'><%=scheduleName%><br/>(<%=startTime%>-<%=endTime%>)</td>
								<% } %>
								<td><%=(list.get(0).size() != 0) && (list.get(0).size() > j)
										? list.get(0).get(j).getUserName()+"<br/>"+list.get(0).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(1).size() != 0) && (list.get(1).size() > j)
										? list.get(1).get(j).getUserName()+"<br/>"+list.get(1).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(2).size() != 0) && (list.get(2).size() > j)
										? list.get(2).get(j).getUserName()+"<br/>"+list.get(2).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(3).size() != 0) && (list.get(3).size() > j)
										? list.get(3).get(j).getUserName()+"<br/>"+list.get(3).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(4).size() != 0) && (list.get(4).size() > j)
										? list.get(4).get(j).getUserName()+"<br/>"+list.get(4).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(5).size() != 0) && (list.get(5).size() > j)
										? list.get(5).get(j).getUserName()+"<br/>"+list.get(5).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
								<td><%=(list.get(6).size() != 0) && (list.get(6).size() > j)
										? list.get(6).get(j).getUserName()+"<br/>"+list.get(6).get(j).getUserTelNo().split(",")[0]
												: ""%></td>
							</tr>
						<%
								i++;
								}
							}
						}
						%>

			</tbody>
		</table>
	<div style="text-align:center;padding:10px" class="tdTitle">
		<a id="pre" class="a-btn-microsoft btn"
			href="javascript:XF.Duty.dutyWeekSearch('<%=preWeek%>')">上周</a>
		<a id="next" class="a-btn-microsoft btn" 
			href="javascript:XF.Duty.dutyWeekSearch('<%=nextWeek%>')">下周</a>
		<%if(rows>0){%>
		<a class="a-btn-microsoft btn"
			href="<%=request.getContextPath()%>/duty/exportDuty?siteId=<%=siteId %>&deptId=<%=deptId %>
			&dutyDate=<%=dutyTime %>">导出</a>
		<% }%>
	</div>
<script>
$('#siteList').combobox({
	url:global.contextPath + '/duty/datagridSites',
	valueField:'siteId',
	textField:'siteName',
	method:'get',
	/* missingMessage:'请选择所属类别', */
	onSelect:function(data){
		var siteId = data.siteId;
		var depturl = global.contextPath + '/duty/datagridDepts?siteId=' + siteId;
		$("#deptList").combobox('reload', depturl);
	},
	onChange:function(){
		$("#deptList").combobox('clear');
    },
});

$('#deptList').combobox({
	url:global.contextPath + '/duty/datagridDepts?siteId=' + '<%=siteId%>',
	valueField:'deptId',
	textField:'deptName',
	method:'get',
});

$('#siteList').combobox('setValue', '<%=siteId%>');
<%-- $('#siteList').combobox('setText', '<%=siteName%>');--%>
 var dept = '<%=deptId%>';
	if(dept!='D999'){
		$('#deptList').combobox('setValue', '<%=deptId%>');
	}else{
		$('#deptList').combobox('setValue', '');
	}
	/* 时间选择器 */
	$("#dutyTime").datebox({
		formatter:XF.dateFormatter,
		parser:XF.dateParse,
		editable:false,
	});
	
	<%if (StringUtils.isNotBlank(dutyTime)) {%>
		$("#dutyTime").datebox('setValue','<%=dutyTime%>'.replace(/-/g,'/'));
	<%
	}%>
</script>